package com.kye.express.business.order;

import android.content.Context;
import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.Nullable;

import com.jakewharton.rxbinding2.view.RxView;
import com.kye.base.DebounceObservableTransformer;
import com.kye.express.R;
import com.kye.express.business.base.BaseAppCompatActivity;
import com.kye.express.business.order_detail.OrderDetailActivity;
import com.kye.express.databinding.ActivityAddOrderSuccessBinding;
import com.kye.express.utils.ToolbarManager;

import io.reactivex.functions.Consumer;

public class AddOrderSuccessActivity extends BaseAppCompatActivity {

    private static final String KEY_ORDER_CODE = "KEY_ORDER_CODE";
    private static final String KEY_ORDER_PHONE = "KEY_ORDER_PHONE";
    private static final String KEY_ORDER_SCORE_CODE = "KEY_ORDER_SCORE_CODE";
    private ActivityAddOrderSuccessBinding mBinding;
    private String phone;
    private String orderCode;

    public static void startActivity(Context context, String phone, String orderCode) {
        Intent intent = new Intent(context, AddOrderSuccessActivity.class);
        intent.putExtra(KEY_ORDER_CODE, orderCode);
        intent.putExtra(KEY_ORDER_PHONE, phone);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mBinding = DataBindingUtil.setContentView(this, R.layout.activity_add_order_success);
        if (savedInstanceState == null) {
            phone = getIntent().getStringExtra(KEY_ORDER_PHONE);
            orderCode = getIntent().getStringExtra(KEY_ORDER_CODE);
        } else {
            phone = savedInstanceState.getString(KEY_ORDER_PHONE);
            orderCode = savedInstanceState.getString(KEY_ORDER_CODE);
        }
        initEvent();
    }

    private void initEvent() {
        ToolbarManager.setToolbar(this, mBinding.toolbar);

        RxView.clicks(mBinding.buttonOrderDetail)
                .compose(new DebounceObservableTransformer<>())
                .compose(bindToLifecycle())
                .subscribe(new Consumer<Object>() {
                    @Override
                    public void accept(Object o) throws Exception {
                        startActivity(
                                OrderDetailActivity.newStartIntent(AddOrderSuccessActivity.this, phone, orderCode, "5")
                        );
                        finish();
                    }
                });

        RxView.clicks(mBinding.buttonFinished)
                .compose(new DebounceObservableTransformer<>())
                .compose(bindToLifecycle())
                .subscribe(new Consumer<Object>() {
                    @Override
                    public void accept(Object o) throws Exception {
                        finish();
                    }
                });
    }

}
